#include "stdafx.h"
#include "ckLandsat.h"
#include "ckRaster.h"
#include "ckRasterBand.h"
#include "ckEnumRasterBand.h"
#include "ckSchema.h"
#include "ckLandsatProperty.h"
#include "ckLandsatNameParser.h"
#include "ckLandsatMetaData.h"
#include "ckLandsatRawMetaData.h"
#include "ckLandsatMetaDataReader.h"

namespace ck
{
	namespace data
	{
		Landsat::Landsat()
		{
			m_bIsOpen = false;
			m_pSchema = NULL;
			m_pMetaData = NULL;
			m_pRawMetaData = NULL;
			m_pProperty = NULL;
		}

		Landsat::~Landsat()
		{
			Cleanup();
		}

		const char* Landsat::GetName()
		{
			return m_strName.c_str();
		}

		z_uint32 Landsat::GetBandCount()
		{
			return m_bandNames.size();
		}

		const char* Landsat::GetBandName(z_uint32 i)
		{
			if (i >= m_bandNames.size())
			{
				return NULL;
			}

			return m_bandNames[i].c_str();
		}

		RESULTCODE Landsat::Open()
		{
			return RC_SUCCESS;
		}

		bool Landsat::IsOpen()
		{
			return m_bIsOpen;
		}

		void Landsat::Close()
		{
			m_bIsOpen = false;
		}

		bool Landsat::Verify()
		{
			return true;
		}

		void Landsat::SetPath(const char* szPath)
		{
			if (!szPath)
			{
				m_strName.clear();
				m_strPath.clear();
			}
			else
			{
				char szName[Z_NAME_MAX];
				memset(szName, 0, Z_NAME_MAX);
				_splitpath(szPath, NULL, NULL, szName, NULL);
				m_strName = szName;
				m_strPath = szPath;

				//////////////////////////////////////////////////////////////////////////
				m_strAngleConffFileName = m_strName + "_ANG.txt";
				m_strMetaDataFileName = m_strName + "_MTL.txt";
				m_strGCPFileName = m_strName + "_GCP.txt";
				m_strReportVerifyFileName = m_strName + "_VER.txt";
				m_strBrowseVerifyFileName = m_strName + "_VER.jpg";
			}
		}

		const char* Landsat::GetPath()
		{
			return m_strPath.c_str();
		}

		Raster* Landsat::GetRaster(z_uint32 i)
		{
			return NULL;
		}

		RasterBand* Landsat::GetBand(z_uint32 i)
		{
			return NULL;
		}

		RasterBand* Landsat::GetBandByKey(const char* szKey)
		{
			return NULL;
		}

		const char*	Landsat::GetBandKey(z_uint32 i)
		{
			return NULL;
		}

		Raster*	Landsat::OpenRaster(z_uint32 i)
		{
			return NULL;
		}

		EnumRasterBand*	Landsat::GetBands()
		{
			EnumRasterBand* pBands = new EnumRasterBand();

			z_uint32 idx = 0;
			RasterBand* pBand = NULL;
			std::vector<RasterBand*>::iterator iter;
			for (iter = m_bands.begin(); iter != m_bands.end(); iter++,idx++)
			{
				pBand = *iter;
				if (!pBand)
				{
					pBand = GetBand(idx);
				}
				if (pBand)
				{
					pBand->AddRef();
					pBands->Add(pBand);
				}
			}

			return pBands;
		}

		void Landsat::Cleanup()
		{
			std::vector<Raster*>::iterator iter;
			for (iter = m_rasters.begin(); iter != m_rasters.end(); iter++)
			{
				if (*iter)
				{
					(*iter)->Release();
					(*iter) = NULL;
				}
			}
			if (m_pRawMetaData)
			{
				m_pRawMetaData->Release();
				m_pRawMetaData = NULL;
			}
			if (m_pMetaData)
			{
				m_pMetaData->Release();
				m_pMetaData = NULL;
			}
			if (m_pSchema)
			{
				m_pSchema->Release();
				m_pSchema = NULL;
			}
			if (m_pProperty)
			{
				m_pProperty->Release();
				m_pProperty = NULL;
			}
		}

		Schema*	Landsat::GetSchema()
		{
			if (!m_pSchema)
			{
				return m_pSchema;
			}

			return m_pSchema;
		}

		LandsatMeta* Landsat::GetMetaData()
		{
			if (!m_pMetaData)
			{
				return m_pMetaData;
			}

			return m_pMetaData;
		}

		LandsatRawMeta* Landsat::GetRawMetaData()
		{
			return m_pRawMetaData;
		}

		LandsatProperty* Landsat::GetProperty()
		{
			if (!m_pProperty)
			{
				m_pProperty = new LandsatProperty();

				LandsatNameParser parser;
				RESULTCODE rc = parser.Parse(m_strName.c_str(), m_pProperty);
				if (rc != RC_SUCCESS)
				{
					m_pProperty->Release();
					m_pProperty = NULL;
				}
			}

			return m_pProperty;
		}

		const char* Landsat::GetAngleConfficientFileName()
		{
			return m_strAngleConffFileName.c_str();
		}

		const char*	Landsat::GetMetaDataFileName()
		{
			return m_strMetaDataFileName.c_str();
		}

		const char*	Landsat::GetGCPFileName()
		{
			return m_strGCPFileName.c_str();
		}

		const char*	Landsat::GetReportVerifyFileName()
		{
			return m_strReportVerifyFileName.c_str();
		}

		const char*	Landsat::GetBrowseVerifyFileName()
		{
			return m_strBrowseVerifyFileName.c_str();
		}

		EnumString*	Landsat::GetFileNames()
		{
			return NULL;
		}
	}
}


